package com.ruoyi.web.controller.h5.design;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dto.DesignerAddDto;
import com.ruoyi.system.dto.DesignerSeleteDto;
import com.ruoyi.system.service.DesignerService;
import com.ruoyi.system.service.IFavoriteService;
import com.ruoyi.system.vo.DesignerDetailVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/h5/admin/designer")
public class DesignerController {

    @Resource
    private DesignerService designerService;

    @Resource
    private IFavoriteService favoriteService;

    /**
     * 设计师新增
     */
    @PostMapping("/add")
    public AjaxResult addDesigner(@RequestParam("file") MultipartFile file,
                                  @RequestParam(value = "name", required = false) String name,
                                  @RequestParam(value = "employmentAge", required = false) Integer employmentAge,
                                  @RequestParam(value = "caseNumber", required = false) Long caseNumber,
                                  @RequestParam(value = "workTime", required = false) Date workTime,
                                  @RequestParam(value = "recommendeValue", required = false) Integer recommendeValue,
                                  @RequestParam(value = "status", required = false) String status) {
        DesignerAddDto dto = new DesignerAddDto();
        dto.setName(name);
        dto.setEmploymentAge(employmentAge);
        dto.setCaseNumber(caseNumber);
        dto.setWorkTime(workTime);
        dto.setRecommendeValue(recommendeValue);
        dto.setStatus(status);
        return AjaxResult.success(designerService.addDesigner(file,dto));
    }
//    public AjaxResult addDesigner(@RequestBody DesignerAddDto dto){
//        return AjaxResult.success(designerService.addDesigner(dto));
//    }


    /**
     * 设计师编辑
     */
    @PostMapping("/edit")
    public AjaxResult editDesigner(@RequestBody DesignerAddDto dto){
        return AjaxResult.success(designerService.editDesigner(dto));
    }

    /**
     * 设计师模糊搜索
     */
    @PostMapping("/query")
    public AjaxResult queryDesigner(@RequestBody DesignerSeleteDto dto){
        return AjaxResult.success(designerService.queryDesigner(dto));
    }

    /**
     * 设计师推荐值排序（带收藏标识）
     */
    @PostMapping("/condition")
    public AjaxResult queryDesignerByValue(){
        List<DesignerDetailVo> designerList = designerService.queryDesignerByValue();
        
        // 如果用户已登录，添加收藏标识
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        if (userId != null && designerList != null && !designerList.isEmpty()) {
            // 提取设计师ID列表
            List<Long> designerIds = designerList.stream()
                    .map(designer -> {
                        try {
                            return designer.getId();
                        } catch (NumberFormatException e) {
                            return null;
                        }
                    })
                    .filter(Objects::nonNull)
                    .collect(java.util.stream.Collectors.toList());
            
            // 获取用户已收藏的设计师ID列表
            List<Long> favoritedIds = favoriteService.getFavoritedBusinessIds(userId, "designer", designerIds);
            
            // 为每个设计师添加收藏标识
            designerList.forEach(designer -> {
                try {
                    Long designerId = designer.getId();
                    boolean isFavorited = favoritedIds.contains(designerId);
                    designer.setIsFavorited(isFavorited);
                } catch (NumberFormatException e) {
                    designer.setIsFavorited(false);
                }
            });
        }
        
        return AjaxResult.success(designerList);
    }
    /**
     * 设计师详情（带收藏标识）
     */
    @GetMapping("/query/detail")
    public AjaxResult queryDesignerDetail(String id){
        DesignerDetailVo designerDetail = designerService.queryDesignerDetail(id);
        
        // 如果用户已登录，添加收藏标识
        Long userId = SecurityUtils.getH5LoginUser().getUserId();
        if (userId != null && designerDetail != null) {
            try {
                Long designerId = designerDetail.getId();
                boolean isFavorited = favoriteService.isFavorited(userId, "designer", designerId);
                designerDetail.setIsFavorited(isFavorited);
            } catch (NumberFormatException e) {
                designerDetail.setIsFavorited(false);
            }
        }
        
        return AjaxResult.success(designerDetail);
    }

}
